package cn.lijiguo.concurrency.part1.threadobjectmethods;

import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;

/**
 * SleepInterrupted
 * 线程每隔 1 秒钟输出当前时间，被中断 观察
 * Thread.sleep()
 * TimeUnit.SECONDS.sleep()
 * @author lijiguo
 * @date 2020/1/7 13:55
 */
public class SleepInterrupted implements Runnable {

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new SleepInterrupted());
        thread.start();
        Thread.sleep(5000);
        thread.interrupt();
    }

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(LocalDateTime.now());
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                System.out.println("-----> 我被中断了~~~~~");
                e.printStackTrace();
            }
        }
    }
}
